package sk.hackcraft.util.generator;

import java.util.Random;

public class TriangularRandom implements ContinuousRandom
{
	private final double minValue;
	private final double maxValue;
	private final double modeValue;
	
	private final double f;
	
	private final Random random;
	
	public TriangularRandom(long seed, double minValue, double maxValue, double modeValue)
	{
		this.random = new Random(seed);
		
		this.minValue = minValue;
		this.maxValue = maxValue;
		this.modeValue = modeValue;
		
		this.f = (modeValue - minValue) / (maxValue - minValue);
	}
	
	public TriangularRandom(double minValue, double maxValue, double modeValue)
	{
		this(SeedGenerator.getNextSeed(), minValue, maxValue, modeValue);
	}
	
	@Override
	public double next()
	{
		double U = random.nextDouble();
		
		if(U < f)
		{
			return minValue + Math.sqrt(U*(maxValue-minValue)*(modeValue-minValue));
		}
		else
		{
			return maxValue - Math.sqrt((1.0-U)*(maxValue - minValue)*(maxValue-modeValue));
		}
	}
}
